//
//  MPStatusOrginView.swift
//  MPSinaWeibo
//
//  Created by DaVinci on 16/4/13.
//  Copyright © 2016年 DaVinci. All rights reserved.
//

import UIKit
import SnapKit

private let StatusCellMargin: CGFloat = 8

class MPStatusOrginView: UIView {

    
    var statusViewModel: MPStatusViewModel? {
    
        didSet{
        
            //绑定模型数据
            iconView.sd_setImageWithURL(statusViewModel?.headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            //用户等级
            mbRankImageView.image = statusViewModel?.mbImage
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            //设置时间
            //TODO 后期完善
            //            timeLabel.text = status?.created_at
            //            sourceLabel.text = status?.source
            contentLabel.text = statusViewModel?.status?.text
        }
    
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var status: MPStatus? {
    
        didSet{
        
           //绑定模型数据
            let headImageUrl = NSURL(string: status?.user?.profile_image_url ?? "")
            iconView.sd_setImageWithURL(headImageUrl)
           
            nameLabel.text = status?.user?.name
            
            //用户等级
            let mbType = status?.user?.mbrank ?? 0
            
            var mbImage: UIImage?
            if mbType > 0 && mbType < 7 {
               mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
            }
            mbRankImageView.image = mbImage
            
            //用户认证类型
            var verifiedImage: UIImage?
            let verifiedType = status?.user?.verified ?? 0
            switch verifiedType {
            
            case 1:
                verifiedImage = UIImage(named: "avatar_vip")
            case 2,3,5:
                verifiedImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifiedImage = UIImage(named: "avatar_grassroot")
            default:
                verifiedImage = nil
            }
            verifiedImageView.image = verifiedImage
            
            //设置时间
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
        }
    
    }
    
    //MARK: 设置UI
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        //圆角的处理
        iconView.cornerRadius = 20
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
        }
    }
    
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: 15,margin: StatusCellMargin)
}

